package librus.cheeseChaser.tile
{
	import librus.cheeseChaser.events.MoveEvent;
	
	import flash.geom.Point;
	
	import flashx.textLayout.debug.assert;
	
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	import librus.cheeseChaser.util.Assets;
	import librus.cheeseChaser.util.Sprite;
	
	public class Move extends Sprite
	{
		public function Move()
		{
			super();
		}		
		
		private var empty:Image;
		private var over:Image;		
		public var position:Point;
		
		override protected function startup():void
		{
			isListeningToResize = true;
			empty = new Image(Assets.getAtlas().getTexture("empty"));
			addChild(empty);
			
			over = new Image(Assets.getAtlas().getTexture("over"));
			over.visible = false;
			addChild(over);			
			
			empty.height = over.height = empty.width = over.width = Assets.TILE_SIZE;
			empty.x = over.x = empty.y = over.y = 0;
			
			addEventListener(TouchEvent.TOUCH, onTouch);
		}
		
		private var lastPhase:String;
		private function onTouch(event:TouchEvent):void
		{
			var t:Touch = event.getTouch(stage);
			
			switch(t.phase)
			{
				case TouchPhase.BEGAN:
				case TouchPhase.ENDED:					
					over.visible = !over.visible;
					empty.visible = !empty.visible;
					
					if (lastPhase == TouchPhase.BEGAN)
						dispatchEvent(new MoveEvent(MoveEvent.MOVE_SELECTED, position));
					break;
			}		
			
			lastPhase = t.phase;
		}		
	}
}